﻿'Imports Lucene.Net.Documents
'Imports Aricie.Collections

'Namespace Aricie.DNN.Modules.LuceneSearch.Business

'    Public Structure LightDocument

'        Public Enum Field
'            Uid
'            HashCode
'            IndexedDate
'            ModifiedDate
'            PubDate
'            SmartValue
'        End Enum

'        Public Uid As String
'        Public HashCode As String
'        Public IndexedDate As DateTime
'        Public ModifiedDate As DateTime
'        Public PubDate As DateTime
'        Public ScoreDocId As Integer
'        Private _SmartValue As Nullable(Of Long)

'        Public ReadOnly Property IsNull() As Boolean
'            Get
'                Return (String.IsNullOrEmpty(Uid))
'            End Get
'        End Property

'        Public ReadOnly Property SmartValue(ByVal smartCoef As Integer) As Long
'            Get
'                If Not _SmartValue.HasValue Then
'                    Dim dateNow As DateTime = Now
'                    Dim ageLastIndexed As TimeSpan = dateNow - Me.IndexedDate
'                    Dim ageLastModified As TimeSpan = dateNow - Me.ModifiedDate
'                    If smartCoef < 2 OrElse ageLastModified = ageLastIndexed Then
'                        Me._SmartValue = ageLastIndexed.Ticks
'                    Else
'                        Dim val As Double = ageLastIndexed.Ticks / smartCoef + ((smartCoef - 1) / smartCoef) * ((ageLastIndexed.Ticks / ageLastModified.Ticks) * ageLastIndexed.Ticks)
'                        Me._SmartValue = Convert.ToInt64(val)
'                    End If
'                End If
'                Return _SmartValue.Value
'            End Get
'        End Property

'        'Public PubHistory As String

'        Public Sub New(ByVal uid As String, ByVal hashcode As String, ByVal indexedDate As DateTime, ByVal modifiedDate As DateTime, ByVal pubDate As DateTime)
'            Me.Uid = uid
'            Me.HashCode = hashcode
'            Me.IndexedDate = indexedDate
'            Me.ModifiedDate = modifiedDate
'            Me.PubDate = pubDate
'        End Sub

'        Public Shared Function AsDictionary(ByVal results As LuceneQueryResults, ByVal closeSearcher As Boolean) As SerializableDictionary(Of String, LightDocument)
'            Dim toReturn As New SerializableDictionary(Of String, LightDocument)
'            Dim dicoScoreDocIds As New Dictionary(Of String, Integer)
'            Dim dicos As Dictionary(Of String, String()) = results.AsDictionaries(closeSearcher, LuceneGeneralField.Uid, dicoScoreDocIds, _
'                            LuceneGeneralField.HashCode, _
'                            LuceneGeneralField.IndexedDate, _
'                            LuceneGeneralField.ModifiedDate, _
'                            DnnSearchItemField.PubDate)
'            Dim tempLightDoc As New LightDocument

'            For Each arrayEntry As KeyValuePair(Of String, String()) In dicos
'                tempLightDoc.ScoreDocId = dicoScoreDocIds(arrayEntry.Key)
'                tempLightDoc.Uid = arrayEntry.Key
'                tempLightDoc.HashCode = arrayEntry.Value(0)
'                If String.IsNullOrEmpty(arrayEntry.Value(1)) Then
'                    tempLightDoc.IndexedDate = DateTime.MinValue
'                Else
'                    tempLightDoc.IndexedDate = DateTools.StringToDate(arrayEntry.Value(1))
'                End If
'                If String.IsNullOrEmpty(arrayEntry.Value(2)) Then
'                    tempLightDoc.ModifiedDate = tempLightDoc.IndexedDate
'                Else
'                    tempLightDoc.ModifiedDate = DateTools.StringToDate(arrayEntry.Value(2))
'                End If
'                If String.IsNullOrEmpty(arrayEntry.Value(3)) Then
'                    tempLightDoc.PubDate = tempLightDoc.ModifiedDate
'                Else
'                    tempLightDoc.PubDate = DateTools.StringToDate(arrayEntry.Value(3))
'                End If
'                toReturn.Add(tempLightDoc.Uid, tempLightDoc)
'            Next
'            Return toReturn
'        End Function

'        Public Shared ReadOnly NoIndexed As New LightDocument("", "", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue)

'        Public Shared Function GetComparer(ByVal field As LightDocument.Field, Optional ByVal smartCoef As Integer = -1) As IComparer(Of LightDocument)
'            Return New LightDocComparer(field, smartCoef)

'        End Function

'        Private Class LightDocComparer
'            Implements IComparer(Of LightDocument)

'            Public Sub New(ByVal field As LightDocument.Field, ByVal smartCoef As Integer)
'                Me._Field = field
'                Me._SmartCoef = smartCoef
'            End Sub

'            Private _Field As LightDocument.Field
'            Private _SmartCoef As Integer



'            Public Function Compare(ByVal x As LightDocument, ByVal y As LightDocument) As Integer Implements System.Collections.Generic.IComparer(Of LightDocument).Compare
'                Select Case Me._Field
'                    Case Field.Uid
'                        Return String.Compare(x.Uid, y.Uid)
'                    Case Field.HashCode
'                        Return String.Compare(x.HashCode, y.HashCode)
'                    Case Field.IndexedDate
'                        Return Date.Compare(x.IndexedDate, y.IndexedDate)
'                    Case Field.ModifiedDate
'                        Return Date.Compare(x.ModifiedDate, y.ModifiedDate)
'                    Case Field.PubDate
'                        Return Date.Compare(x.PubDate, y.PubDate)
'                    Case Field.SmartValue
'                        Return SmartValueCompare(x, y)
'                End Select
'            End Function

'            Public Function SmartValueCompare(ByVal x As LightDocument, ByVal y As LightDocument) As Integer
'                'Dim smartResultDiff As Int64 = Convert.ToInt64((Math.Sqrt(x.SmartValue(Me._SmartCoef)) - Math.Sqrt(y.SmartValue(Me._SmartCoef))))
'                Dim toReturn As Int32 = 0

'                If Not x.IsNull AndAlso Not y.IsNull Then
'                    If x.SmartValue(Me._SmartCoef) > y.SmartValue(Me._SmartCoef) Then
'                        toReturn = 1
'                    ElseIf x.SmartValue(Me._SmartCoef) < y.SmartValue(Me._SmartCoef) Then
'                        toReturn = -1
'                    Else 'x.SmartValue(Me._SmartCoef) == y.SmartValue(Me._SmartCoef)
'                        toReturn = 0
'                    End If
'                Else
'                    'if one or more lightDocs is Null
'                    If x.IsNull AndAlso y.IsNull Then
'                        toReturn = 0
'                    ElseIf x.IsNull Then
'                        toReturn = -1
'                    ElseIf y.IsNull Then
'                        toReturn = 1
'                    End If
'                End If

'                Return toReturn
'            End Function
'        End Class

'    End Structure

'    'Public Class IndexingDictionaries


'    '    Public Sub New(ByVal results As LuceneQueryResults, ByVal closeSearcher As Boolean)
'    '        Dim dicos As Dictionary(Of String, Dictionary(Of String, String)) = results.AsDictionaries(closeSearcher, _
'    '                                                LuceneGeneralField.Uid, _
'    '                                                LuceneGeneralField.IndexedDate, _
'    '                                                DnnSearchItemField.PubDate, _
'    '                                                LuceneGeneralField.HashCode, _
'    '                                                LuceneGeneralField.ModifiedDate)
'    '        Me.IndexedDateDico = results.AsDictionary(Of String, DateTime)(dicos, LuceneGeneralField.IndexedDate, Nothing, AddressOf DateTools.StringToDate)
'    '        Me.PubDateDico = results.AsDictionary(Of String, DateTime)(dicos, DnnSearchItemField.PubDate, Nothing, AddressOf DateTools.StringToDate)
'    '        Me.HashCodeDico = results.AsDictionary(Of String, String)(dicos, LuceneGeneralField.Uid, Nothing, Nothing)
'    '    End Sub

'    '    Public IndexedDateDico As Dictionary(Of String, DateTime)
'    '    Public PubDateDico As Dictionary(Of String, DateTime)
'    '    Public HashCodeDico As Dictionary(Of String, String)
'    '    Public ModifiedDateDico As Dictionary(Of String, String)

'    'End Class
'End Namespace